export default {
	methods: {
		// 跳转到指定页面
		jumpTo(url, params = {}) {
			let tabBar = null;
			const pages = getCurrentPages();
			const currentPage = pages[pages.length - 1];
			if (currentPage && currentPage.$getApp) {
				tabBar = currentPage.$getApp().$children[0].$refs.tabbar;
			}
			if (tabBar && tabBar.list) {
				const targetPage = tabBar.list.find(tab => url.indexOf(tab.pagePath) !== -1);
				if (targetPage) {
					// 目标页面在 tabbar 中
					uni.switchTab({
						url: targetPage.pagePath
					});
					return;
				}
			}
			// 目标页面不在 tabbar 中
			uni.navigateTo({
				url: this.addParamsToUrl(url, params)
			});
		},
		// 将参数添加到 URL 中
		addParamsToUrl(url, params) {
			let queryString = '';
			for (let key in params) {
				if (params.hasOwnProperty(key)) {
					queryString += `${key}=${params[key]}&`;
				}
			}
			if (queryString) {
				queryString = queryString.slice(0, -1);
				if (url.indexOf('?') !== -1) {
					url += `&${queryString}`;
				} else {
					url += `?${queryString}`;
				}
			}
			return url;
		},
		//跳转后关闭原页面
		redirectTo(url) {
			uni.redirectTo({
				url: url
			});
		}
	}
};